

#include "StringConst.h"

#include "Char.h"

using namespace Pan;

unsigned int StringConst::parse_unsigned_decimal_number(const wchar_t *str, unsigned long long *pnumber)
{
    if (str == nullptr)
        return 0;

    if (pnumber == nullptr)
        return 0;

    *pnumber = 0;
    unsigned int i;
    for (i = 0; str[i] != '\0'; i++)
    {
        Char ch(str[i]);
        if (ch.is_number())
        {
            *pnumber *= 10;
            *pnumber += ch.value() - '0';
        }
        else
        {
            break;
        }
    }
    return i;
}

bool StringConst::is_begin_with(const wchar_t *str) const
{

    // 两个指针都非空,往下

    unsigned int i(0);
    while (str[i] != L'\0' && this->_p[i] != L'\0')
    {
        // 找到不相同的字符
        if (str[i] != this->_p[i])
        {
            return false;
        }
        i++;
    }

    // str到了末尾
    return str[i] == L'\0';
}

bool StringConst::is_end_with(const wchar_t *str) const
{

    // 两个指针都非空,往下

    // 移动各自的下标到字符串的末尾
    unsigned long i1(0), i2(0);
    while (this->_p[i1] != L'\0')
        ++i1;
    while (str[i2] != L'\0')
        ++i2;

    while (i1 > 0 && i2 > 0)
    {
        --i1, --i2;

        if (this->_p[i1] != str[i2])
            return false; // 找到不相同的字符
    }
    return i2 <= 0;
}

bool StringConst::equals(const wchar_t *str) const
{
    // 遍历字符串的每个字符
    unsigned int i(0);
    while (this->_p[i] != L'\0' && str[i] != L'\0')
    {
        // 发现不相同的字符
        if (this->_p[i] != str[i])
            return false;
        i++;
    }

    // 循环退出时,字符串到了末尾

    // 同时到达末尾则形同
    return this->_p[i] == L'\0' && str[i] == L'\0';
}

bool StringConst::equals_ignore_case(const wchar_t *str) const
{

    Char a;
    Char b;

    for (int i = 0; true; ++i)
    {
        a = this->_p[i];
        b = str[i];

        if (a.is_null() || b.is_null())
            break;

        a.to_lower();
        b.to_lower();

        if (a != b)
            return false;
    }

    return a.is_null() && b.is_null();
}

unsigned long StringConst::calc_length() const
{
    unsigned long i(0);
    while (this->_p[i] != L'\0')
        i++;
    return i;
}

int StringConst::first_index_of(wchar_t ch, int len) const
{
    if (len > 0)
    {
        for (int i = 0; i < len; i++)
        {
            if (this->_p[i] == ch)
                return i;
        }
        return -1;
    }
    else
    {
        for (int i = 0; this->_p[i] != L'\0'; i++)
        {
            if (this->_p[i] == ch)
                return i;
        }
        return -1;
    }
}

int StringConst::last_index_of(wchar_t ch, int len) const
{
    int &i(len); // 节省内存
    if (i <= 0)
    {
        for (i = 0; this->_p[i] != L'\0'; ++i)
            ;
    }

    while (i >= 0)
    {
        if (this->_p[i] == ch)
            return i;
        --i;
    }
    return -1;
}

unsigned int StringConst::count(wchar_t ch, int len) const
{
    unsigned int ret(0); // 返回值,字符出现的次数

    if (len <= 0)
    {
        for (len = 0; this->_p[len] != L'\0'; ++len)
            ;
        len -= 1;
    }

    while (len >= 0)
    {
        if (this->_p[len] == ch)
        {
            ++ret;
        }
        --len;
    }

    return ret;
}

int StringConst::first_index_of(const wchar_t *str) const
{

    StringConst cs;
    for (int i = 0; this->_p[i] != L'\0'; ++i)
    {
        cs = this->_p + i;
        if (cs.is_begin_with(str))
            return i;
    }
    return -1;
}
